package com.spc.car.web.controller.sys;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.entity.sys.SysMessage;
import com.spc.car.data.domain.message.po.SysMessagePagePO;
import com.spc.car.data.service.sys.SysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spc.car.web.controller.BaseController;

import javax.validation.Valid;


/***
 *  系统内部消息
 */


@Api(tags = "系统内部消息")
@Validated
@RestController
@RequestMapping("/message")
public class SysMessageController extends BaseController {

    /**消息类型: F 流程消息 P 项目消息 D 部门消息 S 系统消息*/
//    private static final String type_flow = "F";

    @Autowired
    private SysMessageService sysMessageService;

    @ApiOperation(value = "添加消息", notes = "添加消息")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody SysMessagePagePO sysMessage) {

        SysMessage message = new SysMessage();
        BeanUtils.copyProperties(sysMessage, message);
        message.setCreator(getPrintUser().getUserName());

        boolean result = sysMessageService.save(message);
        return result ? AjaxResult.success("添加消息成功") : AjaxResult.error("添加消息失败");
    }

    @ApiOperation(value = "获取消息", notes = "获取消息")
    @PostMapping("/listMessage")
    public AjaxResult listMessage(@Valid @RequestBody SysMessagePagePO po) {

        QueryWrapper condition =  po.toWrapper();
        condition.eq("receiver_id", getPrintUser().getUserId());
        Page<SysMessage> page = sysMessageService.page(new Page(po.getPage(), po.getPageSize()), condition);
        return AjaxResult.success(page);

    }

    @ApiOperation(value = "阅读标记", notes = "阅读标记")
    @PostMapping("/markRead")
    public AjaxResult updateMarkRead(String ids) {
        sysMessageService.updateMarkReads(getPrintUser().getUserId(),ids);
        return AjaxResult.success("阅读标记");

    }

    @ApiOperation(value = "未读消息总数" , notes = "未读消息总数")
    @PostMapping("/unreadCount")
    public  AjaxResult unreadCount(){
        int result = sysMessageService.unreadCount(getPrintUser().getUserId());
        return  AjaxResult.success("未读消息总数查询成功" , result);
    }


}
